// 加密狗相关操作
import { JSEncrypt } from "jsencrypt";
import Axios from "axios";

export const getJmg = (callback: ({}) => any) => {
  Axios.get("https://localhost:4433/token")
    .then(function (response) {
      const jmg = response.data.token;
      const secret = response.data.secret;
      callback({ jmg, secret });
    })
    .catch(function (error) {
      callback({ error });
      console.log(error);
    });
};

export const encrypt = (text: string, secret: string) => {
  const encryptor = new JSEncrypt();
  const publicKey = secret;
  // const publicKey = `-----BEGIN PUBLIC KEY----- ${jmg.value} -----END PUBLIC KEY-----`
  // const publicKey = '-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB -----END PUBLIC KEY-----'
  // console.log('publicKey',publicKey)
  encryptor.setPublicKey(publicKey);
  return encryptor.encrypt(text);
};
